package oop.core.med;
import oop.core.Scheduler;
import oop.core.Updateable;
import oop.core.map.*;
import oop.core.skill.SkillObject;
import oop.core.entity.*;
import java.util.*;

/**
 * This class is meant for CollisionDetection for skills and entities.
 *	@author DangMH
 */
public class SkillCollisionDetection implements Updateable
{
	private List<Entity> entitiesList;						// Map Entities List
	private Avatar avatar;
	private Map<Location, SkillObject> skillObjectMap;		// Map Skill Object List
	private SkillCollisionHandler handler;					// Collision Handler Reference
	
	/**
	 * Empty constructor
	 */
	public SkillCollisionDetection()
	{
		entitiesList = new ArrayList<Entity>();
		avatar = null;
		skillObjectMap = new HashMap<Location, SkillObject>();
		handler = new SkillCollisionHandler();
	}
	
	/**
	 * Parameterized constructor
	 * @param entitiesList
	 * @param skillObjectMap
	 */
	public SkillCollisionDetection(Avatar avatar, List<Entity> entitiesList, Map<Location, SkillObject> skillObjectMap, SkillCollisionHandler handler, Scheduler scheduler)
	{
		this.avatar = avatar;
		this.entitiesList = entitiesList;
		this.skillObjectMap = skillObjectMap;
		this.handler = handler;
		scheduler.registerPersistent( this );
	}
	
	/**
	 * Scheduler interface for delays
	 */
	public void update()
	{
		handler.handleCollisions( detectCollisions() );
	}
	
	/**
	 * Collision algorithm searches Entity list for locations,
	 * and then finds collisions from the skillObjectMap keyList
	 * to return.
	 * @return
	 */
	public List<Location> detectCollisions()
	{
		List<Location> ret = new ArrayList<Location>(), locations = new ArrayList<Location>();

		locations.add( avatar.getLoc() );
		
		for( Entity e : entitiesList )
		{
			locations.add( e.getLoc() );
		}
		
		for( Location l : skillObjectMap.keySet() )
		{
			if( locations.contains(l) )
			{
				ret.add(l);
			}
		}
		
		return ret;
	}
}
